package com.waypolice.manager.service.impl;

import com.waypolice.common.constant.BorrowConstant;
import com.waypolice.common.utils.DateTimeUtil;
import com.waypolice.common.utils.MathUtils;
import com.waypolice.common.utils.PageUtils;
import com.waypolice.manager.service.ArchiveUseCountService;
import com.waypolice.mapper.handwriting.CmsBorRegisterExtMapper;
import com.waypolice.mapper.handwriting.CmsPrintingRegisterMapperSon;
import com.waypolice.pojo.vo.UseCountVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 描述:
 *
 * @author zyp
 * @description 档案利用统计service接口实现类
 * @create 2019-04-01 11:56
 */
@Service
public class ArchiveUseCountServiceImpl implements ArchiveUseCountService {

    private static final Logger logger = LoggerFactory.getLogger(ArchiveUseCountServiceImpl.class);

    @Autowired
    private CmsBorRegisterExtMapper cmsBorRegisterExtMapper;

    @Autowired
    private CmsPrintingRegisterMapperSon cmsPrintingRegisterMapperSon;

    /**
     * @author:zyp
     * @description 获取利用统计情况数据
     * @date: 2019/4/1 17:59
     * @return 
     */
    @Override
    public Object getUseCountTable(int offset,int limit,String firstDate,String lastDate) {
        List<UseCountVo> finalList = getListUseCountVo(firstDate,lastDate);
        PageUtils<UseCountVo> pageUtils = new PageUtils<>();
        List<UseCountVo> useCountVos = pageUtils.pageList(finalList, offset, limit);
        Map<String,Object> result = new HashMap<>(2);
        result.put("rows",useCountVos);
        result.put("total",finalList.size());
        return result;
    }

    /**
     * @author:zyp
     * @description 获取利用情况占比元数据
     * @date: 2019/4/2 11:01
     * @return 
     */
    @Override
    public Object getUseCountPropor(String firstDate, String lastDate) {
        List<UseCountVo> finalList = getListUseCountVo(firstDate,lastDate);
        return finalList;
    }
    
    /**
     * @author:zyp
     * @description 获取利用管理情况数据
     * @date: 2019/4/2 10:59
     * @return 
     */
    private List<UseCountVo> getListUseCountVo(String firstDate,String lastDate){
        //传入参数
        Map<String,Object> map = new HashMap<>();
        map.put("fileDate", StringUtils.isNotBlank(firstDate)?firstDate.replaceAll("-",""):null);
        map.put("lastDate", StringUtils.isNotBlank(lastDate)?lastDate.replaceAll("-",""):null);
        //最终的数据内容
        List<UseCountVo> finalList = new ArrayList<>();
        //保证一个分类具有一个利用统计的数据对象
        Map<String,UseCountVo> useCountMap = new HashMap<>();
        List<Map<String,Object>> volList = cmsBorRegisterExtMapper.getVolCount(map);
        //记录利用的卷数
        for(Map<String,Object> volCount : volList){
            int volcount = MathUtils.object2Int(volCount.get("VOLCOUNT"));
            int taskType = MathUtils.object2Int(volCount.get("TASKTYPE"));
            String fileType = String.valueOf(volCount.get("FILETYPE"));
            //如果当前利用统计中 已经包含了 改业务类型
            UseCountVo useCountVo ;
            if(useCountMap.containsKey(fileType)){
                useCountVo =useCountMap.get(fileType);
            }else{
                useCountVo = new UseCountVo();
            }
            //文书 审计..
            useCountVo.setClassify(fileType);
            switch (taskType){
                case BorrowConstant.AUDIT_TYPE_BORROW :
                case BorrowConstant.AUDIT_TYPE_RENEW:
                    useCountVo.setBorrowVolNum(useCountVo.getBorrowVolNum()+volcount);
                    break;
                case BorrowConstant.AUDIT_TYPE_SEARCH:
                    useCountVo.setSearchVolNum(useCountVo.getBorrowVolNum()+volcount);
                    break;
                default:
            }
            useCountMap.put(fileType,useCountVo);
        }
        //将利用的件数以及当前的人次记录
        List<Map<String,Object>> itemList = cmsBorRegisterExtMapper.getItemCount(map);
        for(Map<String,Object> itemCount : itemList){
            int itemcount = MathUtils.object2Int(itemCount.get("ITEMCOUNT"));
            int taskType = MathUtils.object2Int(itemCount.get("TASKTYPE"));
            String fileType = String.valueOf(itemCount.get("FILETYPE"));
            //如果当前利用统计中 已经包含了 改业务类型
            UseCountVo useCountVo ;
            if(useCountMap.containsKey(fileType)){
                useCountVo =useCountMap.get(fileType);
            }else{
                useCountVo = new UseCountVo();
            }
            //文书 审计..
            useCountVo.setClassify(fileType);
            switch (taskType){
                case BorrowConstant.AUDIT_TYPE_BORROW :
                case BorrowConstant.AUDIT_TYPE_RENEW:
                    useCountVo.setBorrowItemNum(useCountVo.getBorrowItemNum()+itemcount);
                    useCountVo.setBorrowManNum(useCountVo.getBorrowManNum()+1);
                    break;
                case BorrowConstant.AUDIT_TYPE_SEARCH:
                    useCountVo.setSearchItemNum(useCountVo.getBorrowItemNum()+itemcount);
                    useCountVo.setSearchManNum(useCountVo.getSearchManNum()+1);
                    break;
                default:
            }
            useCountMap.put(fileType,useCountVo);
        }
        List<Map<String,Object>> printList = cmsPrintingRegisterMapperSon.getPrintCount(map);
        for(Map<String,Object> printCount : printList){
            String fileType = String.valueOf(printCount.get("FILETYPE"));
            int printcount = MathUtils.object2Int(printCount.get("PRINTCOUNT"));
            //如果当前利用统计中 已经包含了 改业务类型
            UseCountVo useCountVo ;
            if(useCountMap.containsKey(fileType)){
                useCountVo =useCountMap.get(fileType);
            }else{
                useCountVo = new UseCountVo();
            }
            useCountVo.setPrintItemNum(useCountVo.getPrintItemNum()+printcount);
        }
        //遍历利用统计map 进行最后的list数据储存
        for(UseCountVo useCountVo : useCountMap.values()){
            useCountVo.setAllItemNum(useCountVo.getBorrowItemNum()+useCountVo.getSearchItemNum()+useCountVo.getPrintItemNum());
            useCountVo.setAllManNum(useCountVo.getBorrowManNum()+useCountVo.getSearchManNum()+useCountVo.getPrintManNum());
            finalList.add(useCountVo);
        }
        return finalList;
    }
}